package com.qhhc.controller;

import com.qhhc.entity.CarTransaction;
import com.qhhc.service.CarTransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车辆交易Controller
 */
@RestController
@RequestMapping("/api/transactions")
public class CarTransactionController {
    @Autowired
    private CarTransactionService transactionService;

    /**
     * 获取交易列表
     */
    @GetMapping
    public List<CarTransaction> getAllTransactions() {
        return transactionService.getTransactionList(new HashMap<>());
    }

    /**
     * 分页查询交易列表
     */
    @GetMapping("/page")
    public Map<String, Object> getTransactionsByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
            @RequestParam(value = "carId", required = false) Long carId,
            @RequestParam(value = "buyerId", required = false) Long buyerId,
            @RequestParam(value = "dealerId", required = false) Long dealerId,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "paymentStatus", required = false) Integer paymentStatus,
            @RequestParam(value = "paymentMethod", required = false) String paymentMethod) {
        
        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        params.put("carId", carId);
        params.put("buyerId", buyerId);
        params.put("dealerId", dealerId);
        params.put("status", status);
        params.put("paymentStatus", paymentStatus);
        params.put("paymentMethod", paymentMethod);
        
        List<CarTransaction> list = transactionService.getTransactionList(params);
        int total = transactionService.getTransactionCount(params);
        
        Map<String, Object> result = new HashMap<>();
        result.put("data", list);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        
        return result;
    }

    /**
     * 获取交易详情
     */
    @GetMapping("/{id}")
    public CarTransaction getTransactionById(@PathVariable("id") Long id) {
        return transactionService.getTransactionById(id);
    }

    /**
     * 获取用户的交易记录
     */
    @GetMapping("/user/{userId}")
    public List<CarTransaction> getUserTransactions(@PathVariable("userId") Long userId) {
        return transactionService.getUserTransactions(userId);
    }

    /**
     * 获取经销商的交易记录
     */
    @GetMapping("/dealer/{dealerId}")
    public List<CarTransaction> getDealerTransactions(@PathVariable("dealerId") Long dealerId) {
        return transactionService.getDealerTransactions(dealerId);
    }

    /**
     * 获取车辆的交易记录
     */
    @GetMapping("/car/{carId}")
    public List<CarTransaction> getCarTransactions(@PathVariable("carId") Long carId) {
        return transactionService.getCarTransactions(carId);
    }

    /**
     * 添加交易
     */
    @PostMapping
    public int addTransaction(@RequestBody CarTransaction transaction) {
        return transactionService.addTransaction(transaction);
    }

    /**
     * 更新交易
     */
    @PutMapping
    public int updateTransaction(@RequestBody CarTransaction transaction) {
        return transactionService.updateTransaction(transaction);
    }

    /**
     * 更新交易状态
     */
    @PutMapping("/{id}/status/{status}")
    public int updateTransactionStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return transactionService.updateTransactionStatus(id, status);
    }

    /**
     * 更新支付状态
     */
    @PutMapping("/{id}/payment/{paymentStatus}")
    public int updatePaymentStatus(@PathVariable("id") Long id, @PathVariable("paymentStatus") Integer paymentStatus) {
        return transactionService.updatePaymentStatus(id, paymentStatus);
    }

    /**
     * 删除交易
     */
    @DeleteMapping("/{id}")
    public int deleteTransaction(@PathVariable("id") Long id) {
        return transactionService.deleteTransaction(id);
    }

    /**
     * 批量删除交易
     */
    @DeleteMapping("/batch/{ids}")
    public int batchDeleteTransactions(@PathVariable("ids") String ids) {
        String[] idArray = ids.split(",");
        Long[] transactionIds = new Long[idArray.length];
        for (int i = 0; i < idArray.length; i++) {
            transactionIds[i] = Long.valueOf(idArray[i]);
        }
        return transactionService.deleteTransactionByIds(transactionIds);
    }
} 